public class Sort {
    //直接插入算法
    public static void InsertSotrt(int[] arr) {
        int i = 1;
        while (i < arr.length) {
            int j = i - 1;
            int tmp = arr[i];
            while (j >= 0) {
                if (tmp < arr[j]) {
                    arr[j + 1] = arr[j];
                    j--;
                } else {
                    arr[j + 1] = tmp;
                    break;
                }
            }
            arr[j + 1] = tmp;
            i++;
        }
    }

    //希尔排序
    public static void shellSort(int[] array) {
        int gap = array.length;
        while (gap > 1) {
            gap /= 2;
            shell(array, gap);
        }
    }

    private static void shell(int[] array, int gap) {
        int i = gap;
        while (i < array.length) {
            int j = i - gap;
            int tmp = array[i];
            while (j >= 0) {
                if (tmp < array[j]) {
                    array[j + gap] = array[j];
                    j -= gap;
                } else {
                    array[j + gap] = tmp;
                    break;
                }
            }
            array[j + gap] = tmp;
            i++;
        }
    }

    //选择排序
    public static void selectSort(int[] array) {
        for (int i = 0; i < array.length; i++) {
            int minIndex = i;
            for (int j = i + 1; j < array.length; j++) {
                if (array[j] < array[minIndex]) {
                    minIndex = j;
                }
            }
            swap(array, i, minIndex);
        }
    }

    private static void swap(int[] array, int minIndex, int i) {
        int tmp = array[minIndex];
        array[minIndex] = array[i];
        array[i] = tmp;
    }

    //Optimization
    public static void selectSortOptimization(int[] array) {
        int left = 0;
        int right = array.length - 1;

        while (left < right) {
            int minIndex = left;
            int maxIndex = left;
            for (int i = left + 1; i <= right; i++) {
                if (array[i] > array[maxIndex]) {
                    maxIndex = i;
                }
                if (array[i] < array[minIndex]) {
                    minIndex = i;
                }
            }
            swap(array, left, minIndex);
            if (array[left] == array[maxIndex]) {
                maxIndex = minIndex;
            }
            swap(array, right, maxIndex);
            left++;
            right--;
        }
    }

    // 堆排序
    public static void heapSort(int[] array) {
        createHeap(array);

        int end = array.length - 1;
        while (end > 0) {
            swap(array, 0, end);
            shifDown(array, 0, end);
            end--;
        }
    }

    private static void createHeap(int[] array) {
        for (int i = (array.length - 1 - 1) / 2; i >= 0; i--) {
            shifDown(array, i, array.length);
        }
    }

    private static void shifDown(int[] array, int parent, int usedSize) {
        int child = parent * 2 + 1;
        while (child < usedSize) {
            if (child + 1 < usedSize && array[child] < array[child + 1]) {
                child++;
            }
            if (array[child] > array[parent]) {
                Swap(array, child, parent);
                parent = child;
                child = parent * 2 + 1;
            } else {
                break;
            }
        }
    }

    private static void Swap(int[] array, int child, int parent) {
        int tmp = array[child];
        array[child] = array[parent];
        array[parent] = tmp;
    }


    // 冒泡排序
    public static void bubbleSort(int[] array) {
        for (int i = 0; i < array.length; i++) {
            boolean flg = false;
            for (int j = 0; j < array.length - i - 1; j++) {
                if (array[j] > array[j + 1]) {
                    swap(array, j, j + 1);
                    flg = true;
                }
            }
            if (!flg) {
                break;
            }
        }
    }

    // 快速 挖坑法
    public static void quickSort(int[] array) {
        quick(array, 0, array.length - 1);
    }

    private static void quick(int[] array, int begin, int end) {
        if (begin >= end) {
            return;
        }

        //如果数据只有7个的话 直接插入排序
        if (begin + end < 7) {
            InsertSortquick(array, begin, end);
            return;
        }

        //三数取中
        int mid = getMiddleNum(array, begin, end);
        swap(array, begin, mid);

        int pirot = partitionHoare(array, begin, end); // 霍尔法
//        int pirot = partitionDipPit(array, begin, end); // 挖坑法
//        int pirot = partition(array, begin, end); // 前后指针法
        quick(array, begin, pirot - 1);
        quick(array, pirot + 1, end);
    }

    private static void InsertSortquick(int[] array, int begin, int end) {
        int i = begin + 1;
        while (i <= end) {
            int j = i - 1;
            int tmp = array[i];
            while (j >= begin) {
                if (tmp < array[j]) {
                    array[j + 1] = array[j];
                    j--;
                } else {
                    array[j + 1] = tmp;
                    break;
                }
            }
            array[j + 1] = tmp;
            i++;
        }
    }

    private static int getMiddleNum(int[] array, int begin, int end) {
        int mid = (begin + end) / 2;
        if (array[begin] > array[end]) {
            if (array[mid] > array[begin])
                return begin;
            else if (array[begin] > array[mid])
                return mid;
            else
                return end;
        } else {
            if (array[mid] > array[begin])
                return mid;
            else if (array[begin] > array[mid])
                return begin;
            else
                return end;
        }
    }

    // 挖坑法
    private static int partitionDipPit(int[] array, int left, int right) {
        int tmp = array[left];
        while (left < right) {
            while (left < right && array[right] >= tmp) {
                right--;
            }
            array[left] = array[right];
            while (left < right && array[left] <= tmp) {
                left++;
            }
            array[right] = array[left];
        }
        array[left] = tmp;
        return left;
    }

    // 霍尔法
    private static int partitionHoare(int[] array, int left, int right) {
        int key = left;
        while (left < right) {
            while (left < right && array[right] >= array[key]) {
                right--;
            }
            while (left < right && array[left] <= array[key]) {
                left++;
            }
            swap(array, left, right);
        }
        swap(array, left, key);
        return left;
    }

    private static int partition(int[] array, int left, int right) {
        int prev = left;
        int cur = left + 1;

        while (cur <= right) {
            if (array[cur] < array[left] && array[++prev] != array[cur]) {
                swap(array, cur, prev);
            }
            cur++;
        }
        swap(array, left, prev);
        return prev;
    }

    private static void quickNor(int[] array, int begin, int end) {

    }

    // 归并排序---递归
    public static void mergeSort(int[] array){
        merge(array,0,array.length-1);
    }

    private static void merge(int[] array, int begin, int end) {
        if(begin >= end){
            return ;
        }

        int mid = (begin + end) / 2;
        merge(array,begin,mid);
        merge(array,mid+1,end);

        mergeChild(array,begin,mid,end);
    }

    private static void mergeChild(int[] array, int begin,int mid, int end) {
        int[] tmp = new int[end - begin + 1];
        int b1 = begin;
        int e1 = mid;
        int b2 = mid + 1;
        int e2 = end;
        int i = 0;
        while (b1 <= e1 && b2 <= e2){
            if(array[b1] <= array[b2]){
                tmp[i++] = array[b1++];
            }else {
                tmp[i++] = array[b2++];
            }
        }
        while (b1 <= e1){
            tmp[i++] = array[b1++];
        }
        while (b2 <= e2){
            tmp[i++] = array[b2++];
        }
        for (int j = 0; j < i; j++) {
            array[j+begin] = tmp[j];
        }
    }


    // 归并排序---非递归
    public static void mergeSortNor(int[] array){
        int gap = 1;
        while (gap < array.length){
            for (int i = 0; i < array.length; i += 2 * gap) {
                int left = i;
                int mid = left + gap - 1;
                if(mid >= array.length){
                    mid = array.length - 1;
                }
                int right = mid + gap;
                if(right >= array.length){
                    right = array.length - 1;
                }
                mergeChild(array,left,mid,right);
            }
            gap *= 2;
        }
    }

// 计数排序
public static void countSort(int[] array) {
    //1. 找最大值 和 最小值 来确定 计数数组的大小
    int maxVal = array[0];
    int minVal = array[0];
    for (int i = 1; i < array.length; i++) {
        if(array[i] < minVal) {
            minVal = array[i];
        }
        if(array[i] > maxVal) {
            maxVal = array[i];
        }
    }
    int len = maxVal - minVal + 1;
    int[] count = new int[len];

    //2. 遍历原来的数组array把 每个元素 放到对应的计数数组当中 进行计数
    for (int i = 0; i < array.length; i++) {
        int index = array[i];
        count[index-minVal]++;
    }
    //3.依次 遍历计数数组 O(范围)
    int index = 0;
    for (int i = 0; i < count.length; i++) {
        while (count[i] != 0) {
            array[index] = i+minVal;
            index++;
            count[i]--;
        }
    }
}

//    public static void countSort(int[] array){
//        int min = array[0];
//        int max = array[0];
//        for (int i = 0; i < array.length; i++) {
//            if(array[i] < min){
//                min = array[i];
//            }
//            if(array[i] > max){
//                max = array[i];
//            }
//        }
//
//        int[] count = new int[max - min + 1];
//        for (int i = 0; i < array.length; i++) {
//
//        }
//    }
}
